#ifndef RT_USE_OLD_CONN_WRAP

#include "RtNetworkBase.h"
#include "RtConnRlbTcpPdu.h"

CRtConnRlbTcpPduConnReq::CRtConnRlbTcpPduConnReq(
		WORD inConnAttr,
		WORD inChannelId,
		SeqType inAck,
		DWORD inTicket)
		: CRtConnRlbTcpPduBase(RT_CONN_RLBTCP_PDU_CONN_REQ)
		, m_connAttr(inConnAttr)
		, m_channelId(inChannelId)
		, m_ack(inAck)
		, m_ticket(inTicket)
{
}

CRtConnRlbTcpPduConnReq::CRtConnRlbTcpPduConnReq()
		: CRtConnRlbTcpPduBase(RT_CONN_RLBTCP_PDU_CONN_REQ)
{
}

CRtConnRlbTcpPduConnReq::~CRtConnRlbTcpPduConnReq()
{
}

void CRtConnRlbTcpPduConnReq::Encode(CRtMessageBlock& outData)
{
	CRtByteStreamIntel os(outData);
	os << m_type;
	os << m_connAttr;
	os << m_channelId;
	os << m_ack;
	os << m_ticket;
}

void CRtConnRlbTcpPduConnReq::Decode(CRtMessageBlock& inData)
{
	CRtByteStreamIntel is(inData);
	is >> m_connAttr;
	is >> m_channelId;
	is >> m_ack;
	is >> m_ticket;
}

CRtConnRlbTcpPduConnResp::CRtConnRlbTcpPduConnResp(
		RtResult inResult, 
		WORD inChannelId,
		SeqType inAck,
		DWORD inTicket)
		: CRtConnRlbTcpPduBase(RT_CONN_RLBTCP_PDU_CONN_RESP)
		, m_result(inResult)
		, m_channelId(inChannelId)
		, m_ack(inAck)
		, m_ticket(inTicket)
{
}
		
CRtConnRlbTcpPduConnResp::CRtConnRlbTcpPduConnResp()
		: CRtConnRlbTcpPduBase(RT_CONN_RLBTCP_PDU_CONN_RESP)
{
}

CRtConnRlbTcpPduConnResp::~CRtConnRlbTcpPduConnResp()
{
}

void CRtConnRlbTcpPduConnResp::Encode(CRtMessageBlock& outData)
{
	CRtByteStreamIntel os(outData);
	os << m_type;
	os << m_result;
	os << m_channelId;
	os << m_ack;
	os << m_ticket;
}

void CRtConnRlbTcpPduConnResp::Decode(CRtMessageBlock& inData)
{
	CRtByteStreamIntel is(inData);
	is >> m_result;
	is >> m_channelId;
	is >> m_ack;
	is >> m_ticket;
}

CRtConnRlbTcpPduDisconn::CRtConnRlbTcpPduDisconn()
		: CRtConnRlbTcpPduBase(RT_CONN_RLBTCP_PDU_DISCONN)
{
}

CRtConnRlbTcpPduDisconn::~CRtConnRlbTcpPduDisconn()
{
}

void CRtConnRlbTcpPduDisconn::Encode(CRtMessageBlock& outData)
{
	CRtByteStreamIntel os(outData);
	os << m_type;
}

void CRtConnRlbTcpPduDisconn::Decode(CRtMessageBlock& inData)
{
}

CRtConnRlbTcpPduKeepalive::CRtConnRlbTcpPduKeepalive(SeqType inAck)
		: CRtConnRlbTcpPduBase(RT_CONN_RLBTCP_PDU_KEEPALIVE)
		, m_ack(inAck)
{
}

CRtConnRlbTcpPduKeepalive::CRtConnRlbTcpPduKeepalive()
		: CRtConnRlbTcpPduBase(RT_CONN_RLBTCP_PDU_KEEPALIVE)
{
}

CRtConnRlbTcpPduKeepalive::~CRtConnRlbTcpPduKeepalive()
{
}

void CRtConnRlbTcpPduKeepalive::Encode(CRtMessageBlock& outData)
{
	CRtByteStreamIntel os(outData);
	os << m_type;
	os << m_ack;
}

void CRtConnRlbTcpPduKeepalive::Decode(CRtMessageBlock& inData)
{
	CRtByteStreamIntel is(inData);
	is >> m_ack;
}

CRtConnRlbTcpPduData::CRtConnRlbTcpPduData(BYTE inType, CRtMessageBlock* inData, DWORD inDataLen)
		: CRtConnRlbTcpPduBase(inType)
#ifdef RLBTCP_ENABLE_SHORT_DATALEN
		, m_shortLength(inDataLen<0xFF ? (BYTE)inDataLen : 0xFF)
#endif
		, m_ack(0)
		, m_length(inDataLen)
		, m_data(inData)
{
}

CRtConnRlbTcpPduData::CRtConnRlbTcpPduData(BYTE inType)
		: CRtConnRlbTcpPduBase(inType)
#ifdef RLBTCP_ENABLE_SHORT_DATALEN
		, m_shortLength(0)
#endif
		, m_ack(0)
		, m_length(0)
		, m_data(NULL)
{
}

CRtConnRlbTcpPduData::~CRtConnRlbTcpPduData()
{
	Release();
}

void CRtConnRlbTcpPduData::Encode(CRtMessageBlock& outData)
{
	CRtByteStreamIntel os(outData);
	os << m_type;
	switch(m_type)
	{
	case RT_CONN_RLBTCP_PDU_DATA_WITH_ACK:
	case RT_CONN_RLBTCP_PDU_DATA_NEED_ACK:
		os << m_ack;

	case RT_CONN_RLBTCP_PDU_DATA:
#ifdef RLBTCP_ENABLE_SHORT_DATALEN
		os << m_shortLength;
		if (m_shortLength==0xFF)
#endif
			os << m_length;
		outData.Append(m_data);
		return;

	default:
		RT_ASSERTE(FALSE);
	}
}

void CRtConnRlbTcpPduData::DecodeFixLength(CRtMessageBlock& inData)
{
	CRtByteStreamIntel is(inData);
	switch(m_type)
	{
	case RT_CONN_RLBTCP_PDU_DATA_WITH_ACK:
	case RT_CONN_RLBTCP_PDU_DATA_NEED_ACK:
		is >> m_ack;

	case RT_CONN_RLBTCP_PDU_DATA:
#ifdef RLBTCP_ENABLE_SHORT_DATALEN
		is >> m_shortLength;
#else
		is >> m_length;
#endif
		return;

	default:
		RT_ASSERTE(FALSE);
	}
}

CRtConnRlbTcpPduRtt::CRtConnRlbTcpPduRtt(DWORD inTimestamp)
: CRtConnRlbTcpPduBase(RT_CONN_RLBTCP_PDU_RTT)
, m_timestamp(inTimestamp)
{
}

CRtConnRlbTcpPduRtt::~CRtConnRlbTcpPduRtt()
{
}

void CRtConnRlbTcpPduRtt::Encode(CRtMessageBlock& outData)
{
	CRtByteStreamIntel os(outData);
	os << m_type;
	os << m_timestamp;
}

void CRtConnRlbTcpPduRtt::Decode(CRtMessageBlock& inData)
{
	CRtByteStreamIntel is(inData);
	is >> m_timestamp;
}

#endif // !RT_USE_OLD_CONN_WRAP
